package com.pine.dcts.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.pine.app.module.security.core.userdetails.UserDetails;
import com.pine.dcts.common.DctsConstant;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.Date;
import java.util.Set;

@Setter
@Getter
@TableName("dcts_user")
public class User extends BaseEntity implements UserDetails {

    /**
     * id
     */
    @TableId(value = "id",type = IdType.AUTO)
    private Integer userId;
    /**
     * 登录账号  可能为自定义 可能为手机号
     *
     */
    @NotBlank(message = "登录账号不能为空")
    @Length(min = 3,max = 32,message = "登录账号长度至少3个字符，最大不超过32个字符")
    private String loginName;
    /**
     * 用户名
     */
    @NotBlank(message = "用户名不能为空")
    @Length(min = 2,max = 32,message = "用户名长度至少2个字符，最大不超过32个字符")
    private String username;
    /**
     * 密码
     */
    @TableField(value = "user_pwd")
    @JsonIgnore
    private String password;
    /**
     * 手机号码
     */
    @Pattern(regexp = "^1\\d{10}$",message = "手机号码格式错误")
    private String userTel;
    /**
     * 登录状态  0禁用 1正常 2锁定
     */

    @TableField(value = "user_status")
    private Integer status;
    /**
     *  用户类型  区分是人员类型
     */

    private Integer type;

    /**
     * 创建时间  时间戳
     */

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createDate;

    @TableField(exist = false)
    private Set<String> roles;

    @TableField(exist = false)
    private String roleStr;
    @TableField(exist = false)
    private Integer[] roleIds;

    @JsonIgnore
    @Override
    public Set<String> getResources() {
        return null;
    }

    @Override
    public Set<String> getRoles() {
        return roles;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    @JsonIgnore
    public boolean isAccountNonLocked() {
        if(status==null){
            return false;
        }
        return !status.equals(DctsConstant.STATUS_TYPE.LOCKED);
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    public boolean isEnabled() {
        if(status==null){
            return false;
        }
        return !status.equals(DctsConstant.STATUS_TYPE.NORMAL);
    }

    @Override
   public  String getId(){
        return String.valueOf(this.userId);
    }
}
